package Arrays;

/**
 * 作者:hfj
 * 功能:219.存在重复元素Ⅱ
 * 日期: 2025/10/28 19:10
 */
public class ContainsNearbyDuplicate {

    public static void main(String[] args) {
        int[] nums = {1,2,3,1,2,3};
        int k = 2;
        System.out.println(containsNearbyDuplicate1(nums,k));
    }

    //超出时间异常
    public static boolean containsNearbyDuplicate1(int[] nums, int k) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i+1; j < nums.length; j++) {
                if (nums[i] == nums[j]){
                    if (Math.abs(i - j) <= k){
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public static boolean containsNearbyDuplicate(int[] nums, int k){
        for (int i = 0; i < nums.length; i++) {
            for (int j = i+1; j < nums.length && j <= i + k; j++) {
                if (nums[i] == nums[j]){
                    return true;
                }
            }
        }
        return false;
    }
}
